package com.bjb.vr.monitor.controller;

import com.bjb.vr.monitor.ao.SwAlarmAo;
import com.bjb.vr.monitor.constant.AlarmConstant;
import com.bjb.vr.monitor.constant.ServiceConstant;
import com.bjb.vr.monitor.properties.AlarmProperties;
import com.bjb.vr.monitor.properties.EmailProperties;
import com.bjb.vr.monitor.util.EmailUtil;
import com.bjb.vr.monitor.util.SwAlarmSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/monitor/v1/")
public class SkywalkingController {

    @Autowired
    private EmailUtil emailUtil;

    @Autowired
    private AlarmProperties alarmProperties;

    @Autowired
    private EmailProperties emailProperties;


    /**
     * 接收skywalking服务的告警通知并发送至邮箱
     */
    @PostMapping("/skywalking/alarm")
    public void swAlarm(@RequestBody List<SwAlarmAo> alarmList) {
        if (alarmProperties.getType().equals(ServiceConstant.SEND_DINGDING)) {
            // 钉钉消息
            SwAlarmSender.sendDingDingMsg(alarmList, AlarmConstant.SKY_WALKING);
        } else if (alarmProperties.getType().equals(ServiceConstant.SEND_MAIL)) {
            // 邮件消息
            SwAlarmSender.sendEmailMsg(alarmList, emailUtil, emailProperties.getReceivers(), AlarmConstant.SKY_WALKING);
        } else if (alarmProperties.getType().equals(ServiceConstant.SEND_ALL)) {
            // 钉钉消息 + 邮件消息
            SwAlarmSender.sendDingDingMsg(alarmList, "服务性能监控报警");
            SwAlarmSender.sendEmailMsg(alarmList, emailUtil, emailProperties.getReceivers(), AlarmConstant.SKY_WALKING);
        }
    }
}
